﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;

namespace Twipler.Data.Entity.Twipler
{
    public enum SettingType
    {
        ApplicationInternal = 1
    }

    public enum ValueType
    {
        String = 0,
        Integer = 1
    }

    public class Setting : Base
    {       
        [EditOrder(10)]
        public virtual SettingType SettingType { get; set; }

        [EditOrder(20)]
        public virtual string Code { get; set; }

        [EditOrder(30)]
        public virtual ValueType ValueType { get; set; }

        [EditOrder(40)]
        [EditStringType(EditStringType.Types.ValueType)]
        public virtual string SettingValue 
        {
            get
            {
                return _settingValue;
            }

            set
            {
                _settingValue = value;

                if ((SettingType == SettingType.ApplicationInternal) && (Code == "LoggingEnabled"))
                {
                    App.Config.IsLoggingEnabled = (SettingValue == "1");
                }
            }
        }
        private string _settingValue;

        public override bool UserCanModify()
        {
            return App.Current.Visitor.HasRole(VisitorRole.SuperAdmin);
        }

        public override bool UserCanCreate()
        {
            return App.Current.Visitor.HasRole(VisitorRole.SuperAdmin);
        }
    }

    public class Validator
    {
        public static bool IsValid(ValueType type, string text)
        {
            switch (type)
            {
                case ValueType.Integer:
                    int i;
                    return int.TryParse(text, out i);

                case ValueType.String:
                default:
                    return true;
            }
        }
    } 
}
